package com.sky.controller.admin;


import com.sky.exception.FileUploadException;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

import java.util.UUID;
@Api(tags = "文件上传")
@RequestMapping("/admin/common")
@RestController
@Slf4j
public class UploadController {

    @Autowired
    private AliOssUtil aliOssUtil;

    /**
     * 上传文件
     * @param file
     * @return
     */
    @ApiOperation("文件上传")
    @PostMapping("upload")
    public Result uploadfile(MultipartFile file){
        try {
            String originalFilename = file.getOriginalFilename();
            String newFileNAME = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
            String upload = aliOssUtil.upload(file.getBytes(), newFileNAME);
            return Result.success(upload);
        } catch (IOException e) {
            throw new FileUploadException("文件上传异常");
        }

    }

}
